$(function () {
    /**全部初始化**/
//    init_modal("acage","qacage");
    var data = '';
    init_plan(data);
    init_typeNum();
});

function init_typeNum() {
    $.getJSON(ctx+"SaleHelp/getTypeNum",{},function (r) {
        var data = r.data;
        var today = data.today;
        var seven = data.seven;
        var already = data.already;
        var wei = data.wei;
        var yi = data.yi;
        $("#wei").empty().append('<i class="fa fa-flag fa-fw m-r-5"></i>未执行&emsp;&emsp;&emsp;&nbsp; '+wei);
        $("#today").empty().append('今天&emsp;&emsp;&emsp;&emsp;&emsp;&nbsp; '+today);
        $("#seven").empty().append('未来7天&emsp;&emsp;&emsp;&nbsp;&nbsp;&nbsp;'+seven);
        $("#already").empty().append('已过期&emsp;&emsp;&emsp;&emsp;&nbsp;'+already);
        $("#yi").empty().append('<svg class="bi bi-check-circle" width="1em" height="1em" viewBox="0 0 16 16" fill="currentColor" xmlns="http://www.w3.org/2000/svg">\n' +
            '                       <path fill-rule="evenodd" d="M8 15A7 7 0 1 0 8 1a7 7 0 0 0 0 14zm0 1A8 8 0 1 0 8 0a8 8 0 0 0 0 16z"/>\n' +
            '                       <path fill-rule="evenodd" d="M10.97 4.97a.75.75 0 0 1 1.071 1.05l-3.992 4.99a.75.75 0 0 1-1.08.02L4.324 8.384a.75.75 0 1 1 1.06-1.06l2.094 2.093 3.473-4.425a.236.236 0 0 1 .02-.022z"/>\n' +
            '                   </svg>&nbsp;已执行 &emsp;&emsp;&emsp;&nbsp;'+yi);
    });
}

/**初始化所有的模板**/
function init_modal(acage,qacage) {
    if (acage!=""){
        //加载个人的模板
        $.post(ctx+"SaleHelp/getModalGrouplist",{cSaleMtype:1},function (r) {
            if (r.code == 0){
                var msg = r.msg;
                $("#acage").empty();
                $.each(msg,function (index,val) {
                    var randomid= $JuXue.randomChart();
                    var childeren = val.childrens;
                    var ul  = "";
                    $.each(childeren,function (index,val) {
                        ul += "<li uuid ="+val.cSaleUuid+"><a href=\"javascript:;\" onclick='selectPersonModal(this)'  class=\"p-3 p-l-15\">"+val.cSaleTitle+"</a></li>";
                    });
                    var selectGroup = "<div uuid='"+val.cSaleUuid+"'><h3 class=\"panel-title \">\n" +
                        "   <span class=\"height-25 text-left\">\n" +
                        "     <button class=\"accordion-toggle btn btn-link accordion-toggle-styled p-0 m-0 p-l-10 collapsed\"  data-toggle=\"collapse\" data-parent=\"#acage\" href='#"+randomid+"'>\n" +
                        "           <i class=\"fa fa-angle-right pull-left\"></i>\n" +
                        "     </button>\n" +
                        "     <button href=\"#\" onclick='selectPersonGroup(this)'  class=\"btn btn-link width-150 text-left\">"+val.cSaleTitle+"</button>\n" +
                        "     <button href=\"#\" onclick='updatePersonGroup(this)'  class=\"btn btn-link btn-xs width-30 text-right\"><i class=\"fa fa-pencil\"></i></button>\n" +
                        "   </span>\n" +
                        "</h3>\n" +
                        "<hr class=\"m-0\"/>\n" +
                        "<div id="+randomid+" class=\"panel-collapse collapse \">\n" +
                        " <ul class=\"nav nav-pills nav-stacked nav-sm panel-body p-t-5 m-0 p-b-5 \">\n" +ul+
                        " </ul>\n" +
                        "</div></div>";
                    $("#acage").append(selectGroup);
                });
            }  else{
                $JuXue.n_danger("加载个人模板失败");
            }
        });
    }
    if (qacage!=""){
        //加载企业的模板
        $.post(ctx+"SaleHelp/getModalGrouplist",{cSaleMtype:0},function (r) {
            if (r.code == 0){
                var msg = r.msg;
                $("#qacage").empty();
                $.each(msg,function (index,val) {
                    var randomid= $JuXue.randomChart();
                    var childeren = val.childrens;
                    var ul  = "";
                    $.each(childeren,function (index,val) {
                        ul += "<li uuid ="+val.cSaleUuid+"><a href=\"javascript:;\" onclick='selectCompnyModal(this)'  class=\"p-3 p-l-15\">"+val.cSaleTitle+"</a></li>";
                    });
                    var selectGroup = "<div uuid='"+val.cSaleUuid+"'><h3 class=\"panel-title \">\n" +
                        "   <span class=\"height-25 text-left\">\n" +
                        "     <button class=\"accordion-toggle btn btn-link accordion-toggle-styled p-0 m-0 p-l-10 collapsed\" data-toggle=\"collapse\" data-parent=\"#qacage\" href='#"+randomid+"'>\n" +
                        "           <i class=\"fa fa-angle-right pull-left\"></i>\n" +
                        "     </button>\n" +
                        "     <button href=\"#\" onclick='selectCompnyModalGroup(this)'  class=\"btn btn-link width-150 text-left\">"+val.cSaleTitle+"</button>\n" +
                        "     <button href=\"#\" onclick='updateCompnyGroup(this)' class=\"btn btn-link btn-xs width-30 text-right\"><i class=\"fa fa-pencil\"></i></button>\n" +
                        "   </span>\n" +
                        "</h3>\n" +
                        "<hr class=\"m-0\"/>\n" +
                        "<div id="+randomid+" class=\"panel-collapse collapse\">\n" +
                        " <ul class=\"nav nav-pills nav-stacked nav-sm panel-body p-t-5 m-0 p-b-5\">\n" +ul+
                        " </ul>\n" +
                        "</div></div>";
                    $("#qacage").append(selectGroup);
                });
            }  else{
                $JuXue.n_danger("加载个人模板失败");
            }
        });
    }
}
/**初始化个人计划**/
function init_plan(data) {
    var datamsg = $("#selectConnditon").val();
    /**初始化个人的销售计划**/
    $.post(ctx+"SaleHelp/selectScalePlan",{cPlanSelectCondition:datamsg,cPlanType:data},function (r) {
        if (r.code === 0){
            var data = r.msg;
            if(data.length>0){
                $("#list_plan").show();
                $("#nullPlanDiv").hide();
                $("#list_plan").empty();
                var statu = "";
                $.each(data,function (index,val) {
                    var cusname = val.cPlanCname;
                    var num2 = 0;
                    var arr = cusname.match(/[,，]/g);
                    if(arr) num2 = arr.length+1;else{num2=1;}

                    var times = val.cPlanTime;
                    var time = times.substring(0,19);

                    var type = val.cPlanType;
                    var statu = val.cPlanIsimp;

                    var lanel = "";
                    if (type == "1"){
                        lanel = "<label><svg class=\"bi bi-bell-fill text-warning\" width=\"1em\" height=\"1em\" viewBox=\"0 0 16 16\" fill=\"currentColor\" xmlns=\"http://www.w3.org/2000/svg\">\n" +
                            "  <path d=\"M8 16a2 2 0 0 0 2-2H6a2 2 0 0 0 2 2zm.995-14.901a1 1 0 1 0-1.99 0A5.002 5.002 0 0 0 3 6c0 1.098-.5 6-2 7h14c-1.5-1-2-5.902-2-7 0-2.42-1.72-4.44-4.005-4.901z\"/>\n" +
                            "</svg></label> ";
                    }else{
                        lanel = "<label><i class=\"fa fa-phone text-success\"></i></label> ";
                    }
                    var liedit = "";
                    if (datamsg != "1"){
                        liedit = "<li uuid='"+val.cPlanId+"' onclick='seelctPlanDetail(this)'><a href=\"javascript:;\"><i class=\"fa fa-pencil f-s-10 fa-fw m-r-5\"></i>编辑</a></li>\n"
                    }
                    var  li = "<li class=\"list-group-item inverse p-10 m-t-25 m-r-10\" style='box-shadow:2px 2px 8px #888888;border-radius:2px;' \">\n" +
                        "    <div class=\"email-checkbox\">"+lanel+"</div>\n" +
                        "        <div class=\"email-info\">\n" +
                        "            <span class=\"email-time\">"+statu+"</span>\n" +
                        "            <h5 class=\"email-title\">\n" +
                        "              <a href=\"#\" onclick='seelctPlanDetail(this)' uuid='"+val.cPlanId+"' data-toggle=\"ajax\">"+val.cPlanTitle+"</a>\n" +
                        "            </h5>\n" +
                        "            <p class=\"email-desc m-0 p-l-10 p-r-20\">"+val.cPlanDesc+"</p>\n" +
                        "            <div class='cursor col-md-11 col-sm-11' status='"+statu+"' id='"+val.cPlanId+"' onclick='loadPlanCus(this)' uuid='"+val.cPlanId+"'><div class='pull-left'><di class='pull-left'>" +
                        "            <svg class=\"bi bi-person-fill m-t-5 text-primary\" width=\"1.5em\" height=\"1.2em\" viewBox=\"0 0 16 16\" fill=\"currentColor\" xmlns=\"http://www.w3.org/2000/svg\">\n" +
                        "              <path fill-rule=\"evenodd\" d=\"M3 14s-1 0-1-1 1-4 6-4 6 3 6 4-1 1-1 1H3zm5-6a3 3 0 1 0 0-6 3 3 0 0 0 0 6z\"/>\n" +
                        "            </svg><nobr>&nbsp; 客户  <strong class='text-gray-dark'>&emsp;&emsp;&emsp;"+cusname+"</strong> 共"+num2+"个客户</nobr></di>" +
                        "            <br/><div class='pull-left'>" +
                        "             <svg class=\"bi bi-clock-fill m-t-5 m-l-3\" width=\"1em\" height=\"1em\" viewBox=\"0 0 16 16\" fill=\"currentColor\" xmlns=\"http://www.w3.org/2000/svg\">\n" +
                        "              <path fill-rule=\"evenodd\" d=\"M16 8A8 8 0 1 1 0 8a8 8 0 0 1 16 0zM8 3.5a.5.5 0 0 0-1 0V9a.5.5 0 0 0 .252.434l3.5 2a.5.5 0 0 0 .496-.868L8 8.71V3.5z\"/>\n" +
                        "             </svg>"+
                        "            <nobr>&nbsp; 执行时间  <strong class='text-gray-dark'>&emsp;"+time+"</strong></nobr></div></div></div>" +
                        "            <div class=\"btn-group dropdown pull-right\">\n" +
                        "               <label class=\"btn btn-white btn-sm dropdown-toggle\" data-toggle=\"dropdown\"><span class=\"caret\"></span></label>\n" +
                        "                 <ul class=\"dropdown-menu text-left text-sm\">\n" +liedit+
                        "                     <li uuid='"+val.cPlanId+"' onclick='deletePalnMy(this)' ><a href=\"javascript:;\"><i class=\"fa fa-times f-s-10 fa-fw m-r-5\"></i>删除</a></li>\n" +
                        "                 </ul>\n" +
                        "            </div>"+
                        "         </div>\n" +
                        "     </div>\n" +
                        "</li>"
                    $("#list_plan").append(li);
                });
            }else{
                $("#list_plan").hide();
                $("#nullPlanDiv").show();
            }
        }else{
            $JuXue.n_danger("初始化个人销售计划失败");
        }
    });
}

function addPlanNew() {
    $("#newPlanDiv").show();
    $("#nullPlanDiv").hide();
}

function cancelPlan() {
    $("#newPlanDiv").hide();
    $("#nullPlanDiv").show();
}

/**加载计划客户列表**/
function loadPlanCus(btn) {
    var uuid = $(btn).attr("uuid");
    var id = $(btn).attr("id");
    var status = $(btn).attr("status");
    if(status==="已执行"){
        sessionStorage.setItem("planCusBtn","1");
    }else{
        sessionStorage.setItem("planCusBtn","2");
    }
    sessionStorage.setItem("planId",uuid);
    sessionStorage.setItem("PlanCusId",id);
    $.post(ctx+"SaleHelp/loadPlanCus",{},function (r) {
        $("#ajax-content").html("").append(r);
    });
}

/**查看新增计划**/
function seelctPlanDetail(btn) {
    var uuid = $(btn).attr("uuid");
    if (uuid == ""){$JuXue.n_danger("获取详情失败");return;}
    $.post(ctx+"SaleHelp/getScalePlanDetailsd",{},function (r) {
       $("#sale_help_one").empty().append(r).modal("show").draggable();
       $("#editPlanId").val(uuid);
       init_msg(uuid);
    });
}
function  init_msg(uuid) {
    $.post(ctx+"SaleHelp/selectSacleDetail",{cPlanId:uuid},function (r) {
        if (r.code == 0){
            var data = r.msg;
            $("#cPlanTitle").val(data.cPlanTitle);
            $("#cPlanTime").val(data.cPlanTime);
            $("#cPlanDesc").val(data.cPlanDesc);
            $("#zhanshiButtonedit").empty();$("#xialazhanshiedit").empty();
            if (data.cPlanCusid!=null && data.cPlanCname!=null){
                var userid = data.cPlanCusid.split(",");
                var name  = data.cPlanCname.split(",");
                $.each(userid,function (index) {
                   /* if (index > 3){
                        $("#xialazhanshiedit").append("<button href=\"#\" onclick='$(this).remove()' uuid='"+userid[index]+"' class=\"btn btn-default p-3 m-4 \">"+name[index]+"</button>");
                    }else{
                        $("#zhanshiButtonedit").append("<button href=\"#\" onclick='$(this).remove()' uuid='"+userid[index]+"' class=\"btn btn-primary p-3 m-4 \">"+name[index]+"</button>");
                    }*/

                    if (index > 3){
                        $("#xialazhanshiedit").append("<button  uuid='"+userid[index]+"'  class=\"btn btn-outline-secondary p-3 m-4 p-l-5 p-r-5 \">"+name[index]+"</button>");
                    }else{
                        $("#zhanshiButtonedit").append("<button  uuid='"+userid[index]+"'  class=\"btn btn-outline-secondary p-3 m-4 p-l-5 p-r-5 \">"+name[index]+"</button>");
                    }
                });
            }
        } else{
            $JuXue.n_danger("初始化销售计划数据失败")
        }
    });
}

/**编辑删除**/
function deletePalnMy(btn) {
    var uuid = $(btn).attr("uuid");
    if (uuid == ""){$JuXue.n_danger("获取详情失败");return;}
    $JuXue.confirm({
        text: "确定删除销售计划吗？",
        confirmButtonText: "确定删除"
    }, function() {
        $.post(ctx+"SaleHelp/editScalePlanfordelete",{cPlanId:uuid,cPlanIsdelete:"1"},function (r) {
            if (r.code == 0){
                $JuXue.n_success("删除计划成功");
                $('#sale_help_one').empty().modal('hide');
                init_plan();
            }else{
                $JuXue.n_danger(r.msg);
            }
        });
    });
}

/**加载未执行的信息**/
function notexecute(btn) {
    var data = $(btn).attr("data-click");
    $("#selectConnditon").val(data);
    var type = "";
    init_plan(type);
}

function loadPlanList(btn) {
    var data = $(btn).attr("data-click");
    init_plan(data);
}